#pragma once

#include "Server.h"
#include "Client.h"
#include "Buffer.h"
#include "PkgBuf.h"

class CWorkSvr : public CServer
{
public:
    CWorkSvr();
    ~CWorkSvr();

    void *operator new(std::size_t sz);
    void operator delete(void *ptr);

public:
    bool Init(int ilfd);
    bool Destroy();

    // virtual bool Start();
    bool Stop();

    bool ErrorEvent(epoll_event &ee);
    bool InputEvent(epoll_event &ee);
    bool OutputEvent(epoll_event &ee);

    unsigned int GetClientCount() { return m_lstClnt.GetSize(); }
    // 提供给其他进程发送数据到本进程
    int IpcSend(void *pSrc, int iLen);

private:
    bool AcceptEvent();
    bool IpcRecvEvent();
    bool IpcSendEvent();
    bool RecvHandle(ClientNode *pNode);
    bool SendHandler(ClientNode *pNode);

    bool AcceptProc(int icfd, sockaddr &sa, socklen_t sl);
    bool AcceptHandle(int icfd, sockaddr &sa, socklen_t sl);
    bool IpcDataParse();

    ClientNode *CreateClientNode(int ifd);
    void DestroyClientNode(ClientNode *pNode);

private:
    CClientList m_lstClnt; // 连接本Server的客户端列表

    CBuffer<> m_bufIpcRecv;          // IPC接收缓冲
    CBuffer<CSpinLock> m_bufIpcSend; // IPC发送缓冲

    CPkgBuf m_pbIcp;
};
